﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using ExpertMIS.UserControl;
using IBatisNet.Common.Utilities;
using System.Data;

namespace ExpertMIS.Forums
{
    public partial class DomainFAQ : System.Web.UI.Page
    {
        private DomainBroker _domainBroker = new DomainBroker();
        private QuestionBroker _questionBroker = new QuestionBroker();
        private ExpertBroker _expertBroker = new ExpertBroker();
        private PagingSize _pagingSize = new PagingSize();

        private void RefreshDomainInfo(string DomainOID)
        {
            DomainEntity domain = _domainBroker.GetDomainByOID(DomainOID);
            if (domain != null)
            {
                lblDomainName.Text = domain.DomainName;
                lblDomainRemark.Text = domain.Remark;
            }
        }

        private void RefreshRecommendExperts(string DomainOID)
        {
            ExpertBroker broker = new ExpertBroker();

            dlRecommendExperts.DataSource = broker.GetDomainRecommandExperts(DomainOID, 11);
            dlRecommendExperts.DataBind();
        }

        private void RefreshDomainFAQ(string DomainOID)
        {
            BuildPagingSize();

            //if (string.IsNullOrEmpty(q.Text))
            //{
            //    _pagingSize.SearchKey = string.Empty;
            //}
            //else
            //{
            //    _pagingSize.SearchKey = string.Format("F_Title like '%{0}%'", q.Text.Replace("'", "''"));
            //}
            _pagingSize.SortExpression = "F_UPDATEDATE DESC,F_CREATEDATE DESC";

            dlFAQs.DataSource = _questionBroker.GetDomainFAQs(DomainOID, _pagingSize);
            dlFAQs.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Master is Forums)
            {
                (Master as Forums).Subtitle = "常见问题";
            }

            RefreshDomainInfo(Request["oid"]);
            RefreshRecommendExperts(Request["oid"]);
            RefreshDomainFAQ(Request["oid"]);
        }

        protected void dlRecommendExperts_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ExpertEntity entity = e.Item.DataItem as ExpertEntity;
                ucExpertShort ucShort = e.Item.FindControl("ucExpertShort1") as ucExpertShort;
                if (entity != null && ucShort != null)
                {
                    ucShort.Expert = entity;
                }
            }
        }

        #region 分页信息

        private void BuildPagingSize()
        {
            _pagingSize.Limit = 28;

            int pagingStart = 1;
            if (!int.TryParse(Request["start"], out pagingStart) || pagingStart < 1)
            {
                pagingStart = 1;
            }

            _pagingSize.Start = pagingStart;
        }

        protected string GetPriorPageIndex()
        {
            if (_pagingSize.Start - 1 > 0)
                return (_pagingSize.Start - 1).ToString();
            else
                return _pagingSize.Start.ToString();
        }

        protected string GetNextPageIndex()
        {
            if (_pagingSize.Start + 1 <= (int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit))
                return (_pagingSize.Start + 1).ToString();
            else
                return GetLastPageIndex();
        }

        protected string GetLastPageIndex()
        {
            return ((int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit)).ToString();
        }

        protected string GetPageIndex()
        {
            return ((int)System.Math.Ceiling((double)_pagingSize.Total / _pagingSize.Limit)).ToString();
        }

        protected string GetRecordCount()
        {
            if (_pagingSize.Total < 0) return "0";
            return _pagingSize.Total.ToString();
        }

        #endregion

        protected void dlFAQs_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView row = e.Item.DataItem as DataRowView;
                if (null == row) return;

                HyperLink hlExpert = e.Item.FindControl("hlExpert") as HyperLink;
                if (hlExpert != null)
                {
                    ExpertEntity expert = _expertBroker.GetExpertEntityByUserID(row["F_UserOID"].ToString());
                    hlExpert.Text = expert.Name;
                    hlExpert.NavigateUrl = string.Format("{0}{1}", ResolveUrl("~/Forums/ExpertView.aspx?oid="), expert.OID);
                }

                Label lblDate = e.Item.FindControl("lblDate") as Label;
                if (lblDate != null)
                {
                    lblDate.Text = string.Format("{0:yy-MM-dd}", row["F_UpdateDate"]);
                }
            }
        }
    }
}
